<template>
  <div>
      <p class="title">充值</p>

    <div class="box">
      
      <div class="item">
        <p class="left">充值金额:</p>
        <div class="right">
          <input v-model="selectMoney" placeholder="请输入充值金额" class="ui-input">&nbsp;&nbsp;元
        </div>
      </div>

      <div @click="submitNext()" class="submit">立即充值</div>
      <!-- <div @click="agreeClick()" class="agree">
      <i v-show="agree" class="iconfont">&#xe632;&nbsp;&nbsp;</i>
      <i v-show="!agree" class="iconfont">&#xe60f;</i>
      <span class="txt">我已阅读并同意<router-link tag="span" to="/payMessage">《支付服务协议》</router-link></span>
      </div>-->
      <form
        ref="form"
        action="https://cashier.sandpay.com.cn/fastPay/quickPay/index"
        method="post"
        hidden
      >
        <input name="sign" ref="sign">
        <input name="data" ref="data">
        <input name="id" ref="id">
        <input type="submit" ref="submit">
      </form>
    </div>
  </div>
</template>

<script>
// import ReturnTop from "@/components/ReturnTop";
import Axios from "../../common/js/Axios";

export default {
  data() {
    return {
      //单选框
      radio: "wypay",
      agree: true,
      money_num: [
        599,
        1068,
        1896,
        2876,
        3588,
        4257,
        5869,
        6755,
        7429,
        8956,
        9856,
        9952,
        10563,
        12568,
        13985,
        14237,
        15012,
        15826,
        16872,
        17058,
        17524,
        17851,
        18024,
        18236,
        18562
      ],
      moneyIndex: 0,
      selectMoney: 1,
      query: {},
      sign_data: {}
    };
  },
  components: {
    // ReturnTop
  },
  watch: {
    // selectMoney(v) {
    //   let state = false;
    //   for (var i = 0; i < this.money_num.length; i++) {
    //     if (v == this.money_num[i]) {
    //       state = true;
    //       this.moneyIndex = i;
    //     }
    //   }
    //   if (state == false) {
    //     this.moneyIndex = -1;
    //   }
    // }
  },
  methods: {
    //是否同意支付协议
    agreeClick() {
      this.agree = !this.agree;
    },
    moneyClick(index) {
      this.moneyIndex = index;
      this.selectMoney = this.money_num[this.moneyIndex];
    },
    submitNext() {
      if (!this.agree) {
        this.$message("请阅读并同意《支付服务协议》");
        return false;
      }
      let userToken = localStorage.getItem("Authorization");
      Axios.postAxios(
        "/api/payGo",
        {
          money: this.selectMoney,
          pay_type: "bank"
        },
        {
          Authorization: "Bearer " + userToken
        }
      ).then(
        res => {
          console.log(res);
          if (res.data.status) {
            window.location.href = res.data.url;
            // v-model 绑定似乎赶不上提交的速度
            // this.sign_data = res.data.data;
            this.$refs.sign.value = res.data.data.sign;
            this.$refs.data.value = res.data.data.post_data;
            this.$refs.id.value = res.data.data.userid;
            this.$refs.form.action = res.data.data.post_url;
            console.log(this.$refs.form.action);
            this.$refs.submit.click();
          } else {
            this.$message(res.data.msg);
          }
        },
        err => {
          this.$message("系统错误");
        }
      );
    }
  }
};
</script>
<style lang="stylus" scoped>
h2 {
  font-size: 18px;
  font-weight: 800;
  color: #c5965a;
  margin: 10px auto;
}

h1 {
  font-weight: bold;
  font-size: 40px;
  color: red;
}

.title {
  color: #c5965a;
  font-size: 15px;
  font-weight: bold;
  border-bottom: 1px solid #e6e6e6;
  padding-bottom: 5px;
  padding-left: 10px;
  margin: 0 10px;
  line-height: 40px;

  i {
    display: inline-block;
    width: 10px;
    height: 10px;
    background: #a77335;
    border-radius: 100%;
    margin-right: 15px;
  }
}

.title {
  color: #a77335;
  font-size: 15px;
  font-weight: bold;
  border-bottom: 1px solid #e6e6e6;
  padding-bottom: 5px;
  padding-left: 10px;
  margin: 0 10px;
  line-height: 40px;

  i {
    display: inline-block;
    width: 10px;
    height: 10px;
    background: #a77335;
    border-radius: 100%;
    margin-right: 15px;
  }
}

.box {
  width: 400px;
  margin: auto;
  margin-top: 50px;

  .item {
    box-sizing: border-box;
    display: flex;
    align-items: center;
    height: 40px;

    .left, .right {
      flex: 1;
      font-size: 14px;
      color: #000;

      .ui-input {
        border: 1px solid #C5C5C5;
        padding: 2px 5px;
        width: 155px;
        font-size: 14px;
        line-height: 28px;
        height: 28px;
        border-radius: 2px;
        min-width: 50px;
        border-radius: 2px;
        padding: 0 0 0 15px;
        border: 1px solid #C5C5C5;
        resize: none;
        vertical-align: middle;
      }
    }

    .right {
      flex: 3;

      span {
        font-weight: 700;
        color: #FF6600;
      }
    }
  }

  .submit {
    display: block;
    width: 295px;
    height: 35px;
    text-align: center;
    color: #fff;
    border: none;
    background-color: red;
    border-radius: 4px;
    font-size: 14px;
    line-height: 35px;
    margin-top: 30px;
    font-weight: 400;
  }
}
</style>

